import java.util.Stack;

public class Test_8_27 {

    //力扣：20. 有效的括号
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        int i = 0;
        for(; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            }else {
                if(stack.empty()) {
                    return false;
                }else {
                    char top = stack.peek();
                    if(top == '(' && ch == ')' || top == '[' && ch == ']' ||
                            top == '{' && ch == '}'){
                        stack.pop();
                    }else {
                        return false;
                    }
                }
            }
        }
        if(stack.empty()) return true;
        return false;
    }



    //牛客网：JZ31 栈的压入、弹出序列
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();

        int i = 0, j = 0;
        for(; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while(!stack.empty() && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }

        return stack.empty();
    }
}
